<?php
class BudgetModel extends MY_Model {
	
	function BudgetModel() {
		parent::MY_Model ('budget');
	}
	/**
	 * 按照用户id得到预算
	 * @param $user_id
	 */
	function get_budget_by_user_id($user_id) {
		$sql = "select * from budget where user_id = ?";
		$query = $this->db->query($sql, array($user_id));
		return $query->row_array();
	}
	
	function save_budget($data) {
		$budget = $this->get_budget_by_user_id($data['user_id']);
		if ($budget) {
			$this->db->where("user_id", $data['user_id']);
			$this->db->update("budget", $data);
		} else {
			$this->insert($data);
		}
	} 
	
}